#ifndef POSITIONCOUNTER_H
#define POSITIONCOUNTER_H

#include "../Buffer/Buffer.h"
#include "Token.h"

#include <stddef.h>

class PositionCounter {
private:
	size_t row;
	size_t column;

	size_t prevColumn;
public:
	PositionCounter(size_t newRow = 0, size_t newColumn = 0) :
		row(newRow), column(newColumn), prevColumn(0) {
	}
	void handle(char c) {
		if (c == '\n') {
			prevColumn = column;
			++row;
			column = 0;
		} else {
			++column;
		}
	}
	void goBack(size_t count) {
		while (count > 0) {
			goBack();
			--count;
		}
	}
	void goBack() {
		if (column > 1) {
			--column;
		} else {
			if (prevColumn == 65535) {
				throw 65535;  // we dont know the prev column
			}
			column = prevColumn;
			prevColumn = 65535;
			if (row > 0) {
				--row;
			}
		}
	}
	size_t getColumn() {
		return column;
	}
	size_t getRow() {
		return row;
	}
};

#endif
